package com.etc.admin.controller;/*
 * 文件名：com.etc.admin.controller
 * 描述：
 * 作者：蒋丽娟
 * 时间：2019/5/24 10:56
 */

import com.etc.entity.Goods;
import com.etc.entity.ProductDisplay;
import com.etc.entity.Stock;
import com.etc.entity.TwoLevel;
import com.etc.service.GoodsService;
import com.etc.service.StockService;
import com.etc.service.TwoLevelService;
import com.etc.utils.CommonMessage;
import com.etc.utils.PageData;
import org.apache.ibatis.annotations.Param;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("admin.action")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private TwoLevelService twoLevelService;
    @Autowired
    private StockService stockService;
    /**
     *
     * @param page 第几页
     * @param limit 一页显示多少条
     * @param content 模糊查询的内容
     * @return 返回分页的对象
     */
    @RequestMapping("toGoodsList")
    @ResponseBody
    public PageData<Goods>  level( Integer page, Integer limit, String content){
        if (content == null)
            content = "";
        PageData<Goods> pd = goodsService.listGoodsByPage(page,limit,content);
        pd.setCode(0);
        pd.setMsg("ok");
        return pd;
    }

    /**
     * 增加商品的也秒跳转
     * @return 跳转到addGoods.jsp界面
     */
    @RequestMapping("addGoods")
    public String selectByOneLevaelValue(){
        return "admin/addGoods";
    }

    /**
     *
     * @param onelevelid 一级列表的id
     * @return 通过一级ID得到的二级列表的集合
     */
    @RequestMapping("selectByOneLevelId")
    @ResponseBody
    public List<TwoLevel> selectByOneLevaelValue( Integer onelevelid){
        System.out.println(onelevelid);
        List<TwoLevel> twoLevelList=twoLevelService.listTwoLevelByOneLeveId(onelevelid);
        return twoLevelList;
    }

    /**
     *
     * @param request
     * @param file 富文框的文件参数，将图片传来过
     * @return
     * @throws IOException 异常处理
     */
    @PostMapping(value = "uploadImage")
    @ResponseBody
    public String uploadPic( HttpServletRequest request, @Param("file") MultipartFile file) throws IOException {
        //本地存储文件路径
        String rootPath = "D:\\imgs";
        //源文件名字
        String originalFilename = file.getOriginalFilename();
        //新的图片名称
        File newDir = new File(rootPath);
        //新文件
        File newFile = new File(rootPath + "/" + originalFilename);
        //判断目标文件所在的目录是否存在
        if (!newFile.getParentFile().exists()) {
            //如果目标文件所在的目录不存在，则创建父目录
            newFile.getParentFile().mkdirs();
        }
        //将内存中的数据写入磁盘
        file.transferTo(newFile);
        //完整的url
        String picUrl = "/imgs/" + originalFilename;
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map2 = new HashMap<String, Object>();
        map.put("code", 0);//0表示成功，1失败
        map.put("msg", "上传成功");//提示消息
        map.put("data", map2);
        map2.put("src", picUrl);//图片url
        map2.put("title", originalFilename);//图片名称，这个会显示在输入框里
        String result = new JSONObject(map).toString();
        return result;
    }

    /**
     *
     * @param goods 添加商品
     * @return
     */
    @RequestMapping("toAddGoods")
    @ResponseBody
    public CommonMessage addGoods(Goods goods){
        CommonMessage commonMessage=new CommonMessage();
      boolean flag=goodsService.insertSelective(goods);
        if(flag){
            commonMessage.setMsg("添加成功");
        }else{
            commonMessage.setMsg("添加失败");
        }
        return commonMessage;
    }

    /**
     * 修改商品的页面跳转
     * @return 跳转到addGoods.jsp界面
     */
    @RequestMapping("editGoods")
    public ModelAndView editGoods(Integer goodsid){
        ModelAndView mv=new ModelAndView("admin/editGoods");
       Goods goods=goodsService.selectByPrimaryKey(goodsid);
        Integer twolevelid=goods.getTwolevelid();
       TwoLevel twoLevel= twoLevelService.getTwoLevelById(twolevelid);
       Integer onelevelid=twoLevel.getOnelevelid();
        mv.addObject("goods",goods);
        mv.addObject("twoLevel",twoLevel);
        return mv;
    }

    /**
     *
     * @param goods 修改商品信息
     * @return
     */
    @RequestMapping("toEditGoods")
    @ResponseBody
    public CommonMessage toEditGoods(Goods goods){
        CommonMessage commonMessage=new CommonMessage();
        boolean flag=goodsService.updateByPrimaryKeySelective(goods);
        if(flag){
            commonMessage.setMsg("修改成功");
        }else{
            commonMessage.setMsg("修改失败");
        }
        return commonMessage;
    }
    /**
     *
     * @param stock 修改商品库存
     * @return
     */
    @RequestMapping("editGoodsStock")
    @ResponseBody
    public CommonMessage editGoodsStock(Stock stock){
        System.out.println(stock);
        CommonMessage commonMessage=new CommonMessage();
        boolean flag=false;
       Integer goodsid=stock.getGoodsid();
       //如果通过商品id在库存表里查询为null,说明是新上架的商品，要添加到库存里
        if(stockService.selectByPrimaryKey(goodsid)==null){
            flag=stockService.insertSelective(stock);
        }else {
            flag=stockService.updateByPrimaryKeySelective(stock);
        }
        if(flag){
            commonMessage.setMsg("修改成功");
        }else{
            commonMessage.setMsg("修改失败");
        }
        return commonMessage;
    }

    /**
     *
     * @param goodsid 修改商品库存
     * @return
     */
    @RequestMapping("editGoodsSatus")
    @ResponseBody
    public CommonMessage editGoodsSatus(Integer goodsid,Integer goodsstatus){
        if(goodsstatus==0){
            goodsstatus=1;
        }else{
            goodsstatus=0;
        }
        Boolean flag=goodsService.updateBygoodsId(goodsid,goodsstatus);
        CommonMessage commonMessage=new CommonMessage();
        if(flag){
            commonMessage.setMsg("修改成功");
        }else{
            commonMessage.setMsg("修改失败");
        }
        return commonMessage;
    }
}
